using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using log4net;
using NHibernate;
using System.Reflection;
using NHibernate.Mapping.Attributes;
using NHibernate.Cfg;


namespace infoMECHANICA.NHibernateORM.ExampleProgram
{            
    public class Program
    {
        private static readonly ILog logger =
            LogManager.GetLogger("BusinessLogicClient");

        private static ISessionFactory sessionFactory;

        private static void BuildNHibernateSessionFactory()
        {
            Configuration cfg = new Configuration();

            Dictionary<string, string> properties = new Dictionary<string, string>();
            properties.Add("connection.driver_class", "NHibernate.Driver.SQLite20Driver");
            properties.Add("dialect", "NHibernate.Dialect.SQLiteDialect");
            properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
            properties.Add("query.substitutions", "true=1;false=0");
            properties.Add("connection.connection_string", @"Data Source=..\..\NHCB_Example.sqlite;Version=3;New=False;");
            properties.Add("connection.release_mode", "on_close");
            cfg.SetProperties(properties);

            Assembly a = Assembly.Load("infoMECHANICA.NHibernateORM.TestMappings");
            cfg = cfg.AddAssembly(a);
            using (MemoryStream stream = new MemoryStream())
            {
                HbmSerializer.Default.Serialize(stream, a);
                stream.Position = 0;
                cfg.AddInputStream(stream);              
            }            

            //  Now that we have our Configuration object, create a new SessionFactory
            sessionFactory = cfg.BuildSessionFactory();
        }

        static void Main()
        {
            //starting logger and nhibernate
            log4net.Config.XmlConfigurator.Configure();            
            logger.Info("STARTING");
            BuildNHibernateSessionFactory();
            ISession session = sessionFactory.OpenSession();           

            RunExample(new Example1(), session);
            RunExample(new Example2(), session);
            RunExample(new Example3(), session);
            RunExample(new Example4(), session);
            RunExample(new Example5(), session);
            RunExample(new Example6(), session);
            RunExample(new Example7(), session);


            session.Close();            
            logger.Info("STOPPED - press any key");
            Console.ReadKey();
        }

        static void RunExample(IExample example, ISession session)
        {
            logger.Info(String.Format("RUNING EXAMPLE: {0}", example.Title));
            IList result = example.ComposeCriteria().GetExecutableCriteria(session).List();
            logger.Info(String.Format("QUERY COMPOSED - RESULTS FOUND: {0}", result.Count));            
        }
    }
}

